import { globalTool, globalLayer } from '../BQGIS-globalVar'

// 坡度分析
export const BQGIS_slope = {
    init(viewer = window.viewer) {
        if (!globalTool.slope) {
            globalTool.slope = new mars3d.thing.Slope({
                arrow: {
                    scale: 0.3, // 箭头长度的比例（范围0.1-0.9）
                    color: Cesium.Color.YELLOW,
                    width: 15, // 箭头宽度
                    show: true
                },
                tooltip: function (event) {
                    const attr = event.graphic?.attr
                    return `坡度: ${attr.slopeStr1}  (${attr.slopeStr2})<br />坡向: ${attr.direction}°`
                }
            })
            viewer.addThing(globalTool.slope)
            globalTool.slope.on(mars3d.EventType.end, function (event) {
                console.log("分析完成", event)
                // event.data[0] 数组内返回值说明： {
                //     position:position,  //坐标位置
                //     slope: slopeValDou, //度数法值，α(坡度)=arc tan (高程差/水平距离)
                //     slopeStr1: text1,   //度数法值字符串
                //     slopeStr2: text2,   //百分比法值字符串，坡度 = (高程差/水平距离)x100%
                //     direction: slopeAngle //坡向值（0-360度）
                // }
            })
        }

        if (!globalTool.contourLine) {
            // 渲染效果
            globalTool.contourLine = new mars3d.thing.ContourLine({
                contourShow: false, // 是否显示等高线
                shadingType: "none", // 地表渲染效果类型:无nono, 高程 elevation, 坡度slope, 坡向aspect
                shadingAlpha: 0.6 /// 地表渲染的透明度
            })
            viewer.addThing(globalTool.contourLine)
        }
        if (!globalLayer.slopeLayer) {
            globalLayer.slopeLayer = new mars3d.layer.GraphicLayer()
            viewer.addLayer(globalLayer.slopeLayer)
        }
    },
    // 添加矩形
    btnDrawExtent(splitNum) {
        this.clearAll()
        globalLayer.slopeLayer.startDraw({
            type: "rectangle",
            style: {
                color: "#007be6",
                opacity: 0.8,
                outline: false
            },
            success: function (graphic) {
                // 绘制成功后回调
                const positions = graphic.getOutlinePositions(false)
                globalLayer.slopeLayer.clear()

                console.log("绘制坐标为", JSON.stringify(mars3d.LngLatArray.toArray(positions))) // 方便测试拷贝坐标

                globalTool.contourLine.positions = positions

                globalTool.slope.add(positions, {
                    splitNum: splitNum, // splitNum插值分割的个数
                    radius: 1, // 缓冲半径（影响坡度坡向的精度）
                    count: 4 // 缓冲的数量（影响坡度坡向的精度）会求周边(count*4)个点
                })
            }
        })
    },

    // 绘制多边形
    btnDraw(splitNum) {
        this.clearAll()
        globalLayer.slopeLayer.startDraw({
            type: "polygon",
            style: {
                color: "#29cf34",
                opacity: 0.3,
                outline: true,
                outlineColor: "#ffffff",
                clampToGround: true
            },
            success: function (graphic) {
                // 绘制成功后回调
                const positions = graphic.positionsShow
                globalLayer.slopeLayer.clear()

                console.log("绘制坐标为", JSON.stringify(mars3d.LngLatArray.toArray(positions))) // 方便测试拷贝坐标

                globalTool.contourLine.positions = positions

                globalTool.slope.add(positions, {
                    splitNum: splitNum, // splitNum插值分割的个数
                    radius: 1, // 缓冲半径（影响坡度坡向的精度）
                    count: 4 // 缓冲的数量（影响坡度坡向的精度）会求周边(count*4)个点
                })
            }
        })
    },

    // 添加点
    btnDrawPoint() {
        this.clearAll()

        globalLayer.slopeLayer.startDraw({
            type: "point",
            style: {
                color: "#ffff00"
            },
            success: function (graphic) {
                const positions = graphic.positionsShow
                globalLayer.slopeLayer.clear()

                globalTool.slope.add(positions)
            }
        })
    },

    // 改变阴影
    changeShadingType(val) {
        globalTool.contourLine.shadingType = val
    },

    clearAll() {
        globalTool.slope.clear()
        globalTool.contourLine.clear()
        if (viewer.scene.requestRenderMode) {
            viewer.scene.requestRender();
        }
    }
}